
clear 
cd "${path}"
cd Data

* Merge children's data to intergenerational links
use ig_links_nocds_ad, replace
merge 1:1 son_id using sons_data, keep(match) nogen

* Reshape long
	reshape long RELSON AGESONHEAD AGESONWIFE RACEH RACEW EDUSON FUMEMBERSSON KIDSNUMBERSON FAMWEIGHTSON INDWTSON SEQNOSONS FMONEYSON FMSONACC FMSONWACC FMSONWAGEACC intnr, i(son_id) j(year) 

* Labels
label var RELSON "Relationship to head of household (child's household)"
label var AGESONHEAD "Age of head of household (child's household)"
label var AGESONWIFE "Age of 'wife' in household (child's household)"
	label var intnr "Household id of son"
	label var INDWTSON "PSID indiv. longitudinal weight"
label var FAMWEIGHTSON "PSID family weight"

replace AGESONHEAD=. if AGESONHEAD>900
replace AGESONWIFE=. if AGESONWIFE>900
replace AGESONHEAD=. if AGESONHEAD<=0
replace AGESONWIFE=. if AGESONWIFE<=0

	replace AGESONHEAD=AGESONHEAD-1
	replace AGESONWIFE=AGESONWIFE-1

gen SONAGE = . 
replace SONAGE = AGESONWIFE if RELSON == 2
replace SONAGE = AGESONWIFE if RELSON == 20
replace SONAGE = AGESONHEAD if RELSON == 1 
replace SONAGE = AGESONHEAD if RELSON == 10
label var AGESONHEAD "Age of child"

* Generate dummy for single vs non-single headed household

gen head=1 if RELSON==1
replace head=1 if RELSON==10
replace head=0 if head==. & RELSON!=.

gen single=1 if AGESONWIFE==. & head!=.
replace single=0 if AGESONWIFE!=. & head!=.
drop head

*WE CREATE COHORTSON FROM SONAGE
cap drop COHORTSON
gen COHORTSON = ((year+1900)-SONAGE)

ssc install carryforward

    . sort son_id year
    . by son_id (year): carryforward COHORTSON, gen (COHORTSONFW)
    . gen int negyear = -year
    . sort son_id negyear
    . by son_id (negyear): carryforward COHORTSON, gen (COHORTSONBK)
    . sort son_id year
    . replace COHORTSON = COHORTSONFW if mi(COHORTSON) & COHORTSONFW == COHORTSONBK
    
    replace COHORTSON = COHORTSONFW if mi(COHORTSON)
    replace COHORTSON = COHORTSONBK if mi(COHORTSON)

gen FSONAGE = ((year + 1900) - COHORTSON)

* Family income

* CPI-All Urban Consumers (Current Series 2019)

gen RFMONEYSON=FMONEYSON
replace RFMONEYSON=FMONEYSON / 0.3340 if year==67   
replace RFMONEYSON=FMONEYSON / 0.3480 if year==68   
replace RFMONEYSON=FMONEYSON / 0.3670 if year==69   
replace RFMONEYSON=FMONEYSON / 0.3880 if year==70   
replace RFMONEYSON=FMONEYSON / 0.4050 if year==71   
replace RFMONEYSON=FMONEYSON / 0.4180 if year==72   
replace RFMONEYSON=FMONEYSON / 0.4440 if year==73   
replace RFMONEYSON=FMONEYSON / 0.4930 if year==74   
replace RFMONEYSON=FMONEYSON / 0.5380 if year==75   
replace RFMONEYSON=FMONEYSON / 0.5690 if year==76   
replace RFMONEYSON=FMONEYSON / 0.6060 if year==77   
replace RFMONEYSON=FMONEYSON / 0.6520 if year==78   
replace RFMONEYSON=FMONEYSON / 0.7260 if year==79   
replace RFMONEYSON=FMONEYSON / 0.8240 if year==80   
replace RFMONEYSON=FMONEYSON / 0.9090 if year==81   
replace RFMONEYSON=FMONEYSON / 0.9650 if year==82   
replace RFMONEYSON=FMONEYSON / 0.9960 if year==83   
replace RFMONEYSON=FMONEYSON / 1.0390 if year==84   
replace RFMONEYSON=FMONEYSON / 1.0760 if year==85   
replace RFMONEYSON=FMONEYSON / 1.0960 if year==86   
replace RFMONEYSON=FMONEYSON / 1.1360 if year==87   
replace RFMONEYSON=FMONEYSON / 1.1830 if year==88   
replace RFMONEYSON=FMONEYSON / 1.2400 if year==89   
replace RFMONEYSON=FMONEYSON / 1.3070 if year==90   
replace RFMONEYSON=FMONEYSON / 1.3620 if year==91   
replace RFMONEYSON=FMONEYSON / 1.4030 if year==92   
replace RFMONEYSON=FMONEYSON / 1.4450 if year==93   
replace RFMONEYSON=FMONEYSON / 1.4820 if year==94   
replace RFMONEYSON=FMONEYSON / 1.5240 if year==95   
replace RFMONEYSON=FMONEYSON / 1.5690 if year==96   
replace RFMONEYSON=FMONEYSON / 1.6050 if year==97   
replace RFMONEYSON=FMONEYSON / 1.6300 if year==98   
replace RFMONEYSON=FMONEYSON / 1.6660 if year==99   
replace RFMONEYSON=FMONEYSON / 1.7220 if year==100   
replace RFMONEYSON=FMONEYSON / 1.7710 if year==101   
replace RFMONEYSON=FMONEYSON / 1.7990 if year==102   
replace RFMONEYSON=FMONEYSON / 1.8400 if year==103   
replace RFMONEYSON=FMONEYSON / 1.8890 if year==104   
replace RFMONEYSON=FMONEYSON / 1.9530 if year==105   
replace RFMONEYSON=FMONEYSON / 2.0160 if year==106   
replace RFMONEYSON=FMONEYSON / 2.0734 if year==107   
replace RFMONEYSON=FMONEYSON / 2.1530 if year==108   
replace RFMONEYSON=FMONEYSON / 2.1454 if year==109   
replace RFMONEYSON=FMONEYSON / 2.1806 if year==110   
replace RFMONEYSON=FMONEYSON / 2.2494 if year==111   
replace RFMONEYSON=FMONEYSON / 2.2959 if year==112   
replace RFMONEYSON=FMONEYSON / 2.3296 if year==113   
replace RFMONEYSON=FMONEYSON / 2.3674 if year==114   
replace RFMONEYSON=FMONEYSON / 2.3702 if year==115   
replace RFMONEYSON=FMONEYSON / 2.4001 if year==116   
replace RFMONEYSON=FMONEYSON / 2.4512 if year==117   
replace RFMONEYSON=FMONEYSON / 2.5111 if year==118   
replace RFMONEYSON=FMONEYSON / 2.5566 if year==119   
replace RFMONEYSON=RFMONEYSON * 0.3340  // 1967 base year

* Drop outliers
gen RFMONEYSONCLEAN = RFMONEYSON 
replace RFMONEYSONCLEAN = . if RFMONEYSON<100 | RFMONEYSON>150000
	
*We keep the observations only if they are head or wife in the house 
replace RFMONEYSONCLEAN = . if ((RELSON != 1) & (RELSON != 2)) & ((RELSON != 10) & (RELSON != 20))

* Log incomes
gen LRFMONEYSONCLEAN = log(RFMONEYSONCLEAN)

* Education: code missing values
replace EDUSON = . if EDUSON == 99 | EDUSON == 98 | EDUSON == 0

* Education: Copy previous values downwards, then use last observed education status for each child
bysort son_id (year): replace EDUSON = EDUSON[_n-1] if EDUSON>=. 
bysort son_id (year): replace EDUSON = EDUSON[_N]

* NOW WE DROP ALL VALUES OF RACE WHICH ARE NOT VALUABLE: 8, 9 Y 0

replace RACEH = . if (RACEH == 8) | (RACEH == 9) | (RACEH == 0)
replace RACEW = . if (RACEW == 8) | (RACEW == 9) | (RACEW == 0)

* AND WE CREATE THE DUMMIES FOR THE DIFFERENT RACES 

gen WHITE = .

replace WHITE = 1 if RACEH == 1 & (RELSON == 1)
replace WHITE = 1 if RACEH == 1 & (RELSON == 10)
replace WHITE = 1 if RACEW == 1 & (RELSON == 2)
replace WHITE = 1 if RACEW == 1 & (RELSON == 20)

replace WHITE = 0 if RACEH != 1 & (RELSON == 1)
replace WHITE = 0 if RACEH != 1 & (RELSON == 10)
replace WHITE = 0 if RACEW != 1 & (RELSON == 2)
replace WHITE = 0 if RACEW != 1 & (RELSON == 20)

gen FWHITESON = . 
replace FWHITESON = FRACEHSON if ((RELSON == 1) | (RELSON == 10))
replace FWHITESON = FRACEWSON if ((RELSON == 2) | (RELSON == 20))

* Only keep if child is household head or spouse (otherwise we do not observe family income)
keep if RELSON==1 | RELSON==2 | RELSON==10 | RELSON==20 

* Clean up and save
	keep son_id year dad_id mom_id RELSON SEXSON EDUSON SONAGE FSONAGE /// 
	    COHORTSON COHORTSONFW COHORTSONBK FMONEYSON RFMONEYSONCLEAN LRFMONEYSONCLEAN ///
	    SEQNOSONS ER30001 FMSONACC FMSONWACC FMSONWAGE intnr FAMWEIGHTSON INDWTSON single
save "sons_data_clean.dta", replace

